﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBModel
{
    public class DB
    {
        public static readonly DBModelContainer Context = new DBModelContainer();

        public static int Save()
        {
            try
            {
                return Context.SaveChanges();
            }

            catch (DbEntityValidationException dbEx)
             {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        var error = string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                            validationError.ErrorMessage);
                        Trace.TraceInformation(error);
                        throw new ApplicationException(error);
                    }
                }
            }
            return -1;
        }
    }
}
